                                                                                                                     /*
 * File      : application.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      the first version
 * 2013-07-12     aozima       update for auto initial.
 */

/**
 * @addtogroup STM32
 */
/*@{*/

#include <board.h>
#include <rtthread.h>

#ifdef  RT_USING_COMPONENTS_INIT
#include <components.h>
#endif  /* RT_USING_COMPONENTS_INIT */


#ifdef RT_USING_DFS
/* dfs filesystem:ELM filesystem init */
#include <dfs_elm.h>
/* dfs Filesystem APIs */
#include <dfs_fs.h>
#endif


#include "mcx314.h"
/* ------------------- app include ------------ */
#include "ledapp.h"
#include "armcontrolapp.h"
#include "lcdcontrolapp.h"
#include "wifiapp.h"

ALIGN(RT_ALIGN_SIZE)

static rt_uint8_t led_stack[ 512 ];
static rt_uint8_t mechanicalArm_ctrl_stack[ 1024 ];
static rt_uint8_t lcd_ctrl_stack[1024];
static rt_uint8_t wifi_ctrl_stack[1024];

static struct rt_thread led_thread;
static struct rt_thread mechanicalArm_ctrl_thread;
static struct rt_thread lcd_ctrl_thread;
static struct rt_thread wifi_ctrl_thread;

void rt_init_thread_entry(void* parameter)
{
  
#ifdef RT_USING_COMPONENTS_INIT
    /* initialization RT-Thread Components */
    rt_components_init();
#endif

    /* Filesystem Initialization */
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_ELMFAT)
    /* mount sd card fat partition 1 as root directory */
    if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
    {
        rt_kprintf("File System initialized!\n");
    }
    else
        rt_kprintf("File System initialzation failed!\n");
#endif  /* RT_USING_DFS */
    /*init device driver */
    rt_hw_mcx314_init();
    
    rt_device_init_all();
    
    
    /*Initializes the Arm control command message queue*/
    static struct rt_messagequeue arm_mq;
    static char arm_msg_pool[2048];
    
    rt_mq_init(&arm_mq,
               "Arm control mq",
               arm_msg_pool,
               128-sizeof(void*),
               sizeof(arm_msg_pool),
               RT_IPC_FLAG_FIFO);
    
               
               
    /*init mechanical arm control system*/
    rt_err_t result;
    result = rt_thread_init(&mechanicalArm_ctrl_thread,
                            "mechanical arm control",
                            armcontrol_thread_entry,
                            &arm_mq,
                            mechanicalArm_ctrl_stack,
                            sizeof(mechanicalArm_ctrl_stack),
                            15,
                            5);
    if (result == RT_EOK)
    {
      rt_thread_startup(&mechanicalArm_ctrl_thread);
    }
    
    /*------------------------------------*/
    result = rt_thread_init(&lcd_ctrl_thread,
                            "LCD control",
                            lcdcontrol_thread_entry,
                            &arm_mq,
                            lcd_ctrl_stack,
                            sizeof(lcd_ctrl_stack),
                            14,
                            5);
    if (result == RT_EOK)
    {
      rt_thread_startup(&lcd_ctrl_thread);
    }
    /*-------------------------------------*/    
    
    result = rt_thread_init(&wifi_ctrl_thread,
                            "WIFIapp",
                            wifi_thread_entry,
                            &arm_mq,
                            wifi_ctrl_stack,
                            sizeof(wifi_ctrl_stack),
                            17,
                            5);
    if (result == RT_EOK)
    {
      rt_thread_startup(&wifi_ctrl_thread);
    }
    /*-------------------------------------*/        
    
}


int rt_application_init(void)
{
    rt_thread_t init_thread;

    rt_err_t result;

    /* init led thread */
    result = rt_thread_init(&led_thread,
                            "led",
                            led_thread_entry,
                            RT_NULL,
                            (rt_uint8_t*)&led_stack[0],
                            sizeof(led_stack),
                            20,
                            5);
    if (result == RT_EOK)
    {
        rt_thread_startup(&led_thread);
    }

    /*--------------------*/
    
    init_thread = rt_thread_create("init",
                                   rt_init_thread_entry, RT_NULL,
                                   2048, 8, 20);


    if (init_thread != RT_NULL)
        rt_thread_startup(init_thread);
    
    /*---------------------*/
    return 0;
}

/*@}*/
